package com.bjpowernode.jdbc;

import com.mysql.jdbc.Driver;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCTest4 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stam = null;
        try {
            //读取配置文件中的数据
            FileInputStream fis = new FileInputStream("C:\\Users\\曾铖坚\\IdeaProjects\\mysql\\connection\\src\\jdbc.propertise");
            Properties properties = new Properties();
            properties.load(fis);
            String driver = (String) properties.get("driver");
            String url = (String) properties.get("url");
            String user = (String) properties.get("user");
            String password = (String) properties.get("password");
            properties.clone();
            fis.close();
            //1.注册驱动
            Class.forName(driver);
            //2.获取连接
            conn = DriverManager.getConnection(url, user, password);
            //3.获取数据库操作对象
            stam = conn.createStatement();
            //4.执行sql语句
            //int count = stam.executeUpdate("delete from dept where deptno = 40");
            // JDBC中的sql语句不需要提供分号结尾。
            int count = stam.executeUpdate("update dept set dname = '销售部1',loc = '天津1' where deptno = 50");
            System.out.println(count == 1 ? "修改成功" : "修改失败");
            //5.处理查询结果集
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //6.释放支援
            try {
                if (stam != null) {
                    stam.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
